import { IFileItem } from '@/appUtils/fs'

export interface IFileItemSortCondition {
    /** 排序字段 */
    field: keyof IFileItem
    /** 排序顺序 */
    order: 'asc' | 'desc'
}

// 默认排序条件
export const defaultSortConditions: IFileItemSortCondition[] = [
    { field: 'isDir', order: 'desc' },
    { field: 'name', order: 'asc' }
]

// 根据添加日期排序
export const sortByAddTimeCondition: IFileItemSortCondition[] = [
    {
        field: 'createTime',
        order: 'desc'
    }
]

export const sortFileList = (arr: IFileItem[], conditions: IFileItemSortCondition[] = sortByAddTimeCondition) => {
    arr.sort((a, b) => {
        for (let i = 0; i < conditions.length; i++) {
            const condition = conditions[i]
            const field = condition.field
            const order = condition.order
            const valueA = a[field]
            const valueB = b[field]
            if (valueA < valueB) {
                return order === 'asc' ? -1 : 1
            } else if (valueA > valueB) {
                return order === 'asc' ? 1 : -1
            }
        }
        return 0
    })
}
